import tkinter as tk
from tkinter import ttk

from simplefactory.cashfactory import CashFactory
from strategy.cashcontext import CashContext


def initui():
    def click_button():
        result = 0
        money = int(entry_price.get()) * int(entry_count.get())
        # 计算结果

        # 简单工厂
        cashFactory = CashFactory()
        cashSuper = cashFactory.createCashAdapter(comboValue.get())
        # result = cashSuper.acceptCash(money)

        # 策略
        cashContext = CashContext(comboValue.get())
        result = cashContext.getResult(money)

        text.insert('1.0',
                    '单价:' + entry_price.get() + ' 数量:' + entry_count.get() + ' 折扣:' + comboValue.get() + ' 费用:' + str(
                        result) + '\n')

    window = tk.Tk()
    window.title('设计模式-Python')
    window.iconbitmap("icon.ico")
    windowX = window.winfo_screenwidth()
    windowY = window.winfo_screenheight()
    x, y = 450, 300
    # 计算中心坐标
    cen_x = (windowX - x) / 2
    cen_y = (windowY - y) / 2
    # print(cen_x, cen_y)
    # 设置窗体宽高、显示坐标
    window.geometry('%dx%d+%d+%d' % (x, y, cen_x, cen_y))

    comboValue = tk.StringVar()

    label_text = tk.Label(window, text='单价：', font=('宋体', 10))
    label_text.place(x=60, y=10)

    entry_price = tk.Entry(window)
    entry_price.place(x=120, y=10)

    label_text2 = tk.Label(window, text='数量：', font=('宋体', 10))
    label_text2.place(x=60, y=40)

    entry_count = tk.Entry(window)
    entry_count.place(x=120, y=40)

    label_text2 = tk.Label(window, text='折扣：', font=('宋体', 10))
    label_text2.place(x=60, y=70)

    list = ['不打折', '打八折', '满100减20']

    combobox = ttk.Combobox(window, values=list, textvariable=comboValue)
    combobox.place(x=120, y=70)

    button = tk.Button(window, text='确定', font=('宋体', 10), width=8, height=1, command=click_button)
    button.place(x=300, y=10)

    button2 = tk.Button(window, text='重置', font=('宋体', 10), width=8, height=1, command=click_button)
    button2.place(x=300, y=40)

    text = tk.Text(window, height=10, width=44)
    text.place(x=60, y=100)

    window.mainloop()


if __name__ == '__main__':
    initui()
